#include <sdk.h>
#include "astyleconfigdlg.h"
#include <wx/xrc/xmlres.h>
#include <wx/intl.h>
#include <wx/font.h>
#include <wx/radiobut.h>
#include <wx/checkbox.h>
#include <wx/textctrl.h>
#include <wx/combobox.h>
#include <wx/spinctrl.h>
#include <string>
#include <configmanager.h>
#include "dlgformattersettings.h"
#include "asstreamiterator.h"
#include "globals.h"

BEGIN_EVENT_TABLE( AstyleConfigDlg, wxPanel )
  EVT_CHECKBOX( XRCID( "chkBreakLines" ), AstyleConfigDlg::OnBreakLineChange )
  EVT_CHECKBOX( XRCID( "chkBreakBlocks" ), AstyleConfigDlg::OnBreakBlocksChange )
END_EVENT_TABLE()

AstyleConfigDlg::AstyleConfigDlg( wxWindow* parent ) {
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/配置格式化.xrc" ) ;
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "dlgAstyleConfig" ), _T( "wxPanel" ) ) ) {
    return;
  }
  LoadSettings();
}

AstyleConfigDlg::~AstyleConfigDlg() {
}

void AstyleConfigDlg::OnBreakLineChange( wxCommandEvent& event ) {
  XRCCTRL( *this, "txtMaxLineLength", wxTextCtrl )->Enable( event.IsChecked() );
  XRCCTRL( *this, "chkBreakAfterLogical", wxCheckBox )->Enable( event.IsChecked() );
}

void AstyleConfigDlg::OnBreakBlocksChange( wxCommandEvent& event ) {
  XRCCTRL( *this, "chkBreakBlocksAll", wxCheckBox )->Enable( event.IsChecked() );
}

void AstyleConfigDlg::LoadSettings() {
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "astyle" ) );
  XRCCTRL( *this, "chkAttachClasses", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/attach_classes" ), false ) );
  XRCCTRL( *this, "chkAttachExternC", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/attach_extern_c" ), false ) );
  XRCCTRL( *this, "chkAttachNamespaces", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/attach_namespaces" ), false ) );
  XRCCTRL( *this, "chkAttachInlines", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/attach_inlines" ), false ) );
  XRCCTRL( *this, "spnContinuation", wxSpinCtrl )->SetValue( cfg->ReadInt( _T( "/continuation" ), 0 ) );
  XRCCTRL( *this, "chkUseTab", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/use_tabs" ), false ) );
  XRCCTRL( *this, "chkForceUseTabs", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/force_tabs" ), false ) );
  XRCCTRL( *this, "chkIndentCase", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/indent_case" ), false ) );
  XRCCTRL( *this, "chkIndentSwitches", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/indent_switches" ), true ) );
  XRCCTRL( *this, "chkIndentPreprocBlock",  wxCheckBox )->SetValue( cfg->ReadBool( _T( "/indent_preproc_block" ), false ) );
  XRCCTRL( *this, "spnMinConditionalEvent", wxSpinCtrl )->SetValue( cfg->ReadInt( _T( "/min_conditional_indent" ), 2 ) );
  XRCCTRL( *this, "txtMaxInStatementIndent", wxTextCtrl )->SetValue( cfg->Read( _T( "/max_instatement_indent" ), _T( "40" ) ) );
  XRCCTRL( *this, "chkBreakClosing", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/break_closing" ), false ) );
  XRCCTRL( *this, "chkAddBrackets", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/add_brackets" ), true ) );
  XRCCTRL( *this, "chkKeepHeaders", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/keep_headers" ), false ) );
  XRCCTRL( *this, "chkKeepStatements", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/keep_statements" ), false ) );
  XRCCTRL( *this, "chkCloseTemplates", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/close_templates" ), false ) );
  XRCCTRL( *this, "chkBreakLines", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/break_lines" ), false ) );
  XRCCTRL( *this, "txtMaxLineLength", wxTextCtrl )->SetValue( cfg->Read( _T( "/max_line_length" ), _T( "200" ) ) );
  XRCCTRL( *this, "chkBreakAfterLogical", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/break_after_mode" ), false ) );
  if( XRCCTRL( *this, "chkBreakLines", wxCheckBox )->GetValue() ) {
    XRCCTRL( *this, "txtMaxLineLength", wxTextCtrl )->Enable();
    XRCCTRL( *this, "chkBreakAfterLogical", wxCheckBox )->Enable();
  } else {
    XRCCTRL( *this, "txtMaxLineLength", wxTextCtrl )->Disable();
    XRCCTRL( *this, "chkBreakAfterLogical", wxCheckBox )->Disable();
  }
  XRCCTRL( *this, "chkBreakBlocks", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/break_blocks" ), false ) );
  XRCCTRL( *this, "chkBreakBlocksAll", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/break_blocks_all" ), false ) );
  XRCCTRL( *this, "chkPadFirstParenOut", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/pad_first_paren_out" ), false ) );
  XRCCTRL( *this, "chkPadHeader", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/pad_header" ), false ) );
  XRCCTRL( *this, "chkDelEmptyLine", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/delete_empty_lines" ), true ) );
  XRCCTRL( *this, "chkFillEmptyLines", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/fill_empty_lines" ), false ) );
  XRCCTRL( *this, "cmbPointerAlign", wxComboBox )->SetValue( cfg->Read( _T( "/pointer_align" ), _T( "None" ) ) );
  XRCCTRL( *this, "cmbReferenceAlign", wxComboBox )->SetValue( cfg->Read( _T( "/reference_align" ), _T( "None" ) ) );
}

void AstyleConfigDlg::SaveSettings() {
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "astyle" ) );
  cfg->Write( _T( "/attach_classes" ), XRCCTRL( *this, "chkAttachClasses", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/attach_extern_c" ), XRCCTRL( *this, "chkAttachExternC", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/attach_namespaces" ), XRCCTRL( *this, "chkAttachNamespaces", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/attach_inlines" ), XRCCTRL( *this, "chkAttachInlines", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/continuation" ), XRCCTRL( *this, "spnContinuation", wxSpinCtrl )->GetValue() );
  cfg->Write( _T( "/use_tabs" ), XRCCTRL( *this, "chkUseTab", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/force_tabs" ), XRCCTRL( *this, "chkForceUseTabs", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/indent_case" ), XRCCTRL( *this, "chkIndentCase", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/indent_switches" ), XRCCTRL( *this, "chkIndentSwitches", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/indent_preproc_block" ), XRCCTRL( *this, "chkIndentPreprocBlock", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/min_conditional_indent" ), XRCCTRL( *this, "spnMinConditionalEvent", wxSpinCtrl )->GetValue() );
  cfg->Write( _T( "/max_instatement_indent" ), XRCCTRL( *this, "txtMaxInStatementIndent", wxTextCtrl )->GetValue() );
  cfg->Write( _T( "/break_closing" ), XRCCTRL( *this, "chkBreakClosing", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/add_brackets" ), XRCCTRL( *this, "chkAddBrackets", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/keep_headers" ), XRCCTRL( *this, "chkKeepHeaders", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/keep_statements" ), XRCCTRL( *this, "chkKeepStatements", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/close_templates" ), XRCCTRL( *this, "chkCloseTemplates", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/break_lines" ), XRCCTRL( *this, "chkBreakLines", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/max_line_length" ), XRCCTRL( *this, "txtMaxLineLength", wxTextCtrl )->GetValue() );
  cfg->Write( _T( "/break_after_mode" ), XRCCTRL( *this, "chkBreakAfterLogical", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/break_blocks" ), XRCCTRL( *this, "chkBreakBlocks", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/break_blocks_all" ), XRCCTRL( *this, "chkBreakBlocksAll", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/pad_first_paren_out" ), XRCCTRL( *this, "chkPadFirstParenOut", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/pad_header" ), XRCCTRL( *this, "chkPadHeader", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/delete_empty_lines" ), XRCCTRL( *this, "chkDelEmptyLine", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/fill_empty_lines" ), XRCCTRL( *this, "chkFillEmptyLines", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/pointer_align" ), XRCCTRL( *this, "cmbPointerAlign", wxComboBox )->GetValue() );
  cfg->Write( _T( "/reference_align" ), XRCCTRL( *this, "cmbReferenceAlign", wxComboBox )->GetValue() );
}
